package com.xly.webclick.adapter;

import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;


/**
 * Created by amos on 2018/5/25.
 */

public class MergeBindingRecyclerAdapter extends RecyclerView.Adapter<CommonBindingViewHolder> {

    private final MergeList list;

    public MergeBindingRecyclerAdapter(MergeList list) {
        this.list = list;
    }

    @NonNull
    @Override
    public CommonBindingViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        ViewDataBinding binding = DataBindingUtil.inflate(inflater, viewType, parent, false);
        CommonBindingViewHolder viewHolder = new CommonBindingViewHolder(binding.getRoot());
        viewHolder.setBinding(binding);
        return viewHolder;
    }


    @Override
    public void onBindViewHolder(@NonNull CommonBindingViewHolder holder, int position) {
        if (list.size() > 0) {
            MergeList.MergeMapItem item = list.getMergeItems(position);
            holder.getBinding().setVariable(item.brId, list.get(position));
            holder.getBinding().executePendingBindings();
        }
    }

    @Override
    public int getItemViewType(int position) {
        MergeList.MergeMapItem item = list.getMergeItems(position);
        if (item != null) {
            return item.layoutId;
        }
        return super.getItemViewType(position);
    }


    @Override
    public int getItemCount() {
        return list.size();
    }
}
